library(tidyverse)
detach("package:dplyr", unload=TRUE)
## Warning: 'dplyr' namespace cannot be unloaded:
## namespace 'dplyr' is imported by 'tidycensus', 'tigris', 'broom' so cannot be unloaded
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:plyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
library(GGally)
##
## Attaching package: 'GGally'
## The following object is masked from 'package:dplyr':
##
## nasa
library(fpp2)
## Loading required package: forecast
## Loading required package: fma
##
## Attaching package: 'fma'
## The following object is masked from 'package:GGally':
##
## pigs
## The following object is masked from 'package:plyr':
##
## ozone
## Loading required package: expsmooth
theme_set(theme_minimal())
2.1 ts objects
y <- ts(c(123, 39, 78, 52, 110), start = 2012)
2.1 Time plots
data("melsyd")
autoplot(melsyd[, "Economy.Class"]) +
labs(title = "Economy Class Passengers: Melbourne-Sydney",
x = "Year",
y = "Thousands")
autoplot(a10) +
scale_y_continuous(label = dollar_format(prefix = "m$")) +
labs(title = "Antidiabetic drug sales",
y = "",
x = "")
2.4 Seasonal plots
ggseasonplot(a10, year.labels = TRUE, year.labels.left = T) +
scale_y_continuous(labels = dollar_format(suffix = "Million")) +
labs(title = "Seasonal plot: antidiabetic drug sales",
y = "", x = "")
ggseasonplot(a10, polar = T) +
labs(y = "", x= "", title = "plot seasonal plot: antidiabetic drug sales") +
theme(legend.position = "none")
2.5 Seasonal subseries plots
ggsubseriesplot(a10) +
labs(y = "", title = "Seasonal subseries plot: antidiabetic drug sales", x= "") +
scale_y_continuous(labels = dollar_format(suffix = "Million"))
2.6 Scatterplots
autoplot(elecdemand[,c("Demand", "Temperature")], facets = T) +
labs(x = "", y = "", title = "Half hourly electricity demand: Victoria, Australia", caption = "Year: 2014")
tbl_df(elecdemand) %>%
ggplot(aes(Temperature, Demand)) +
geom_point(alpha = .1) +
labs(x = "Temperature (°C)",
y = "Demand (GW)")
autoplot(visnights[, 1:5], facets = T) +
labs(y = "Number of visitor nights each quarter (millions)",
x = "")
See the relationship between these five time series
ggpairs(tbl_df(visnights[, 1:5]))
2.7 Lag plots
beer2 <- window(ausbeer, start = 1992)
gglagplot(beer2)
autoplot(beer2)
gglagplot(a10)
2.8 Autocorrelation
ggAcf(beer2)
aelec <- window(elec, start = 1980)
autoplot(aelec) +
labs(x = "",
y = "GWH")
ggAcf(aelec, lag = 48)
ggAcf(aelec, lag = 12)
2.9 White Noise
Time series that show no autocorrelation are called white noise
set.seed(2019)
y <- ts(rnorm(100))
autoplot(y) + labs(x = "", y = "", title = "White Noise")
ggAcf(y)
For a white noise series, we expect 95% of the spikes in the ACF to lie within \[±2/\sqrt{T}\]
2.9 Exercises